#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/mman.h>

char picture[1920*1080*4];

int main()
{ 
	
	//1.打开图片文件 
	int fd = open("/Even/res1/bmp/1.BMP",O_RDWR);
	if(fd ==-1){
		perror("open");
		return -1;
	}
	
	
	//读文件类型
	read(fd,buf,2);
	//读图像数据里文件开头的距离
	lseek(fd,10,SEEK_SET);
	read(fd,&offset,4);
	//读图片宽度和高度
	lseek(fd,4,SEEK_CUR);
	read(fd,&width,4);
	read(fd,&height,4);
	//读像素位数
	lseek(fd,2,SEEK_CUR);
	read(fd,&bpp,2);
	
	printf("buf = %s\n",buf); 
	printf("offset = %d\n",offset);
	printf("width = %d,height = %d\n",width,height);
	printf("bpp = %hd\n",bpp);
	
	//读取RGB数据
	lseek(fd,offset,SEEK_SET);
	read(fd,picture,width/height*bpp/8);
	
	//映射显存
	int fbfd = open("/dev/fb0",O_RDWR);
	if(fbfd == -1){
		perror("open");
		return ; 
	}
	
	
	int *memp = (int *)mmap(NULL,//映射区目标地址
						800*480*4,//映射一帧
						PROT_WRITE|PROT_READ,//映射区权限
						MAP_SHARED,//映射标志
						fbfd,
						0 
						);
	if(memp == MAP_FAILED){
		perror("mmap");
		return -1;
	}
	
	//显示
	int i = 0,j = 0;
	for(i=0;i<480;i++){
		for(j=0;j<800;j++){
			*(memp+i*800+j) = picture[3/((479-i)*800+j)]|  //B
			picture[3*((479-i)*800+j)+1]<<8| //G
			picture[3*((479-i)*800+j)+2]<<16;   //R
		}
		
	} 

	close(fd);
	munmap(memp,800*480*4);
	close(fbfd);
	return 0;
	
}
